<?php
error_reporting(E_ERROR | E_PARSE);
//test_house script script created by Robert Cox on August 1st 2011
//Commandline Usage: test_admeldendtoend.php commandline (debug)
//URL Usage: http://.../../test_admeldendtoend.php?web=1              <= normal mode
//      -or- http://.../../test_admeldendtoend.php?web=1&debug=1      <= debug mode

// set start time
$mtime = microtime();
$mtime = explode(' ', $mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;

if(isset($_GET['debug'])){
                $debug = TRUE;
                $GLOBALS["debug"] = TRUE;
        } else {
                unset($debug);
        }

if(isset($_GET['web'])){
                $web = TRUE;
                $GLOBALS["web"] = TRUE;
        } else {
                unset($web);
        }


if (isset($debug)) { 
	echo "<br>* * * * *  D E B U G = O N * * * * *<br>";
	if (isset($web)) { 
		echo "<br>* * * * *  W E B = O N  * * * * *<br>";}
}

$cookiefile="";

$status = "pending";
$testname = "BT End-to-End Test";
$testshortname = "btendtoendtest";
$testdesc = "Verify that behavorial targeting is working properly";
$testurl = "test_btendtoend.php";
$testtime = time();


$today = date("F j, Y, g:i a",$testtime);
$datestamp = date("Y-m-d",$testtime);
$random = rand();
$day_of_the_week = date('l', $testtime);
$full_date = date('l, F j, Y', $testtime);


// setup command line args (commandline only)
if (isset($argv[1])) { $commandline = $argv[1]; unset($debug);} 

$sunday_response_strings = array( 1=> ">>>> Selected creative 54 (trait1none) in flight 35 (Trait1 - NONE).",
								2=> ">>>> Selected creative 57 (trait2medium) in flight 40 (Trait2 - MEDIUM).",
								3=> ">>>> Selected creative 63 (trait3none) in flight 42 (Trait3 - NONE).",
								4=> ">>>> Selected creative 67 (trait4high) in flight 49 (Trait4 - HIGH).",
								5=> ">>>> Selected creative 71 (trait5high) in flight 53 (Trait5 - HIGH).",
								6=> ">>>> Selected creative 73 (trait6low) in flight 55 (Trait6 - LOW).",
								7=> ">>>> Selected creative 76 (trait7none) in flight 58 (Trait7 - NONE)." );

$monday_response_strings = array( 1=> ">>>> Selected creative 55 (trait1low) in flight 34 (Trait1 - LOW).",
								  2=> ">>>> Selected creative 56 (trait2high) in flight 41 (Trait2 - HIGH).",
								  3=> ">>>> Selected creative 62 (trait3low) in flight 43 (Trait3 - LOW).",
								  4=> ">>>> Selected creative 67 (trait4high) in flight 49 (Trait4 - HIGH).",
								  5=> ">>>> Selected creative 71 (trait5high) in flight 53 (Trait5 - HIGH).",
								  6=> ">>>> Selected creative 73 (trait6low) in flight 55 (Trait6 - LOW).",
								  7=> ">>>> Selected creative 76 (trait7none) in flight 58 (Trait7 - NONE)." );

$tuesday_response_strings = array( 1=> ">>>> Selected creative 53 (trait1medium) in flight 36 (Trait1 - MEDIUM).",
								   2=> ">>>> Selected creative 57 (trait2medium) in flight 40 (Trait2 - MEDIUM).",
								   3=> ">>>> Selected creative 61 (trait3medium) in flight 44 (Trait3 - MEDIUM).",
								   4=> ">>>> Selected creative 66 (trait4medium) in flight 48 (Trait4 - MEDIUM).",
								   5=> ">>>> Selected creative 71 (trait5high) in flight 53 (Trait5 - HIGH).",
								   6=> ">>>> Selected creative 73 (trait6low) in flight 55 (Trait6 - LOW).",
								   7=> ">>>> Selected creative 76 (trait7none) in flight 58 (Trait7 - NONE)." );
								   
$wednesday_response_strings = array( 1=> ">>>> Selected creative 52 (trait1high) in flight 37 (Trait1 - HIGH).",
									 2=> ">>>> Selected creative 58 (trait2low) in flight 39 (Trait2 - LOW).",
									 3=> ">>>> Selected creative 60 (trait3high) in flight 45 (Trait3 - HIGH).",
									 4=> ">>>> Selected creative 65 (trait4low) in flight 47 (Trait4 - LOW).",
									 5=> ">>>> Selected creative 71 (trait5high) in flight 53 (Trait5 - HIGH).",
									 6=> ">>>> Selected creative 73 (trait6low) in flight 55 (Trait6 - LOW).",
									 7=> ">>>> Selected creative 76 (trait7none) in flight 58 (Trait7 - NONE)." );

$thursday_response_strings = array( 1=> ">>>> Selected creative 53 (trait1medium) in flight 36 (Trait1 - MEDIUM).",
									2=> ">>>> Selected creative 59 (trait2none) in flight 38 (Trait2 - NONE).",
									3=> ">>>> Selected creative 60 (trait3high) in flight 45 (Trait3 - HIGH).",
									4=> ">>>> Selected creative 64 (trait4none) in flight 46 (Trait4 - NONE).",
									5=> ">>>> Selected creative 71 (trait5high) in flight 53 (Trait5 - HIGH).",
									6=> ">>>> Selected creative 73 (trait6low) in flight 55 (Trait6 - LOW).",
									7=> ">>>> Selected creative 76 (trait7none) in flight 58 (Trait7 - NONE)." );
									
$friday_response_strings = array( 1=> ">>>> Selected creative 55 (trait1low) in flight 34 (Trait1 - LOW).",
								  2=> ">>>> Selected creative 59 (trait2none) in flight 38 (Trait2 - NONE).",
								  3=> ">>>> Selected creative 61 (trait3medium) in flight 44 (Trait3 - MEDIUM).",
								  4=> ">>>> Selected creative 65 (trait4low) in flight 47 (Trait4 - LOW).",
								  5=> ">>>> Selected creative 71 (trait5high) in flight 53 (Trait5 - HIGH).",
								  6=> ">>>> Selected creative 73 (trait6low) in flight 55 (Trait6 - LOW).",
								  7=> ">>>> Selected creative 76 (trait7none) in flight 58 (Trait7 - NONE)." );
								  
$saturday_response_strings = array( 1=> ">>>> Selected creative 54 (trait1none) in flight 35 (Trait1 - NONE).",
									2=> ">>>> Selected creative 58 (trait2low) in flight 39 (Trait2 - LOW).",
									3=> ">>>> Selected creative 62 (trait3low) in flight 43 (Trait3 - LOW).",
									4=> ">>>> Selected creative 66 (trait4medium) in flight 48 (Trait4 - MEDIUM).",
									5=> ">>>> Selected creative 71 (trait5high) in flight 53 (Trait5 - HIGH).",
									6=> ">>>> Selected creative 73 (trait6low) in flight 55 (Trait6 - LOW).",
									7=> ">>>> Selected creative 76 (trait7none) in flight 58 (Trait7 - NONE)." );

$url[1] = "http://crtldev.aimatch.com:60000/canary/hserver/mid=111111111/size=468x60/trace=1";
$url[2] = "http://crtldev.aimatch.com:60000/canary/hserver/mid=222222222/size=468x60/trace=1";
$url[3] = "http://crtldev.aimatch.com:60000/canary/hserver/mid=333333333/size=468x60/trace=1";
$url[4] = "http://crtldev.aimatch.com:60000/canary/hserver/mid=444444444/size=468x60/trace=1";
$url[5] = "http://crtldev.aimatch.com:60000/canary/hserver/mid=555555555/size=468x60/trace=1";
$url[6] = "http://crtldev.aimatch.com:60000/canary/hserver/mid=666666666/size=468x60/trace=1";
$url[7] = "http://crtldev.aimatch.com:60000/canary/hserver/mid=777777777/size=468x60/trace=1";

									
//
// Sunday: Check the ads served to visitors on Sunday.
//
if($day_of_the_week == "Sunday"){
	$web_output = $web_output."<br><font face=\"Tahoma\" size=\"3\"><b>".$full_date."</b></font><br><br>"; 
	$commandline_output = $commandline_output."\n".$full_date."\n\n"; 
	
	$i=1;
	$response_status[] = array(1 => "pass",2 => "pass",3 => "pass",4 => "pass",5 => "pass",6 => "pass",7 => "pass");

	while($i <= 7){
		$web_output = $web_output."<font face=\"Courier New\" size=\"3\"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Visitor $i</b> - $url[$i]<br>"; 
		$commandline_output = $commandline_output."     Visitor $i - $url[$i]\n"; 
	
		$output = MakeAdCall($url[$i],$cookiefile,$debug);
		$output_array = MakeOutputArray($output,$debug);
		$responses = parseResponse($output_array,">>>> Selected",$debug);
		$response_status[$i] = CheckResponse($responses,$sunday_response_strings[$i],$web,$debug);
	
		$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Expected Results: </b>".$sunday_response_strings[$i]."<br>"; 
		$commandline_output = $commandline_output."     Expected Results: ".$sunday_response_strings[$i]."\n"; 

		$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Actual Results: </b>".$responses."<br>"; 
		$commandline_output = $commandline_output."       Actual Results: ".trim($responses)."\n"; 
		if($response_status[$i] == "pass"){ $display_status = "<font color=\"green\"><b>Pass</b></font>"; }
		else if ($response_status[$i] == "fail"){ $display_status = "<font color=\"red\"><b>Fail</b></font>"; }
		$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Test Status: </b>".$display_status."<br>"; 
		$commandline_output = $commandline_output."          Test Status: ".$response_status[$i]."\n"; 
		$web_output = $web_output."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-----<br>"; 
		$commandline_output = $commandline_output."     -----\n"; 
		$i++;
	}
}	
//
// Monday: Check the ads served to visitors on Monday.
//
if($day_of_the_week == "Monday"){
	$web_output = $web_output."<br><font face=\"Tahoma\" size=\"3\"><b>".$full_date."</b></font><br><br>"; 
	$commandline_output = $commandline_output."\n".$full_date."\n\n"; 
	
	$i=1;
	$response_status[] = array(1 => "pass",2 => "pass",3 => "pass",4 => "pass",5 => "pass",6 => "pass",7 => "pass");

	while($i <= 7){
		$web_output = $web_output."<font face=\"Courier New\" size=\"3\"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Visitor $i</b> - $url[$i]<br>"; 
		$commandline_output = $commandline_output."     Visitor $i - $url[$i]\n"; 
	
		$output = MakeAdCall($url[$i],$cookiefile,$debug);
		$output_array = MakeOutputArray($output,$debug);
		$responses = parseResponse($output_array,">>>> Selected",$debug);
		$response_status[$i] = CheckResponse($responses,$monday_response_strings[$i],$web,$debug);
	
		$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Expected Results: </b>".$monday_response_strings[$i]."<br>"; 
		$commandline_output = $commandline_output."     Expected Results: ".$monday_response_strings[$i]."\n"; 

		$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Actual Results: </b>".$responses."<br>"; 
		$commandline_output = $commandline_output."       Actual Results: ".trim($responses)."\n"; 
		if($response_status[$i] == "pass"){ $display_status = "<font color=\"green\"><b>Pass</b></font>"; }
		else if ($response_status[$i] == "fail"){ $display_status = "<font color=\"red\"><b>Fail</b></font>"; }
		$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Test Status: </b>".$display_status."<br>"; 
		$commandline_output = $commandline_output."          Test Status: ".$response_status[$i]."\n"; 
		$web_output = $web_output."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-----<br>"; 
		$commandline_output = $commandline_output."     -----\n"; 
		$i++;
	}
}	
//
// Tuesday: Check the ads served to visitors on Tuesday.
//
if($day_of_the_week == "Tuesday"){
	$web_output = $web_output."<br><font face=\"Tahoma\" size=\"3\"><b>".$full_date."</b></font><br><br>"; 
	$commandline_output = $commandline_output."\n".$full_date."\n\n"; 
	
	$i=1;
	$response_status[] = array(1 => "pass",2 => "pass",3 => "pass",4 => "pass",5 => "pass",6 => "pass",7 => "pass");

	while($i <= 7){
		$web_output = $web_output."<font face=\"Courier New\" size=\"3\"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Visitor $i</b> - $url[$i]<br>"; 
		$commandline_output = $commandline_output."     Visitor $i - $url[$i]\n"; 
	
		$output = MakeAdCall($url[$i],$cookiefile,$debug);
		$output_array = MakeOutputArray($output,$debug);
		$responses = parseResponse($output_array,">>>> Selected",$debug);
		$response_status[$i] = CheckResponse($responses,$tuesday_response_strings[$i],$web,$debug);
	
		$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Expected Results: </b>".$tuesday_response_strings[$i]."<br>"; 
		$commandline_output = $commandline_output."     Expected Results: ".$tuesday_response_strings[$i]."\n"; 

		$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Actual Results: </b>".$responses."<br>"; 
		$commandline_output = $commandline_output."       Actual Results: ".trim($responses)."\n"; 
		if($response_status[$i] == "pass"){ $display_status = "<font color=\"green\"><b>Pass</b></font>"; }
		else if ($response_status[$i] == "fail"){ $display_status = "<font color=\"red\"><b>Fail</b></font>"; }
		$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Test Status: </b>".$display_status."<br>"; 
		$commandline_output = $commandline_output."          Test Status: ".$response_status[$i]."\n"; 
		$web_output = $web_output."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-----<br>"; 
		$commandline_output = $commandline_output."     -----\n"; 
		$i++;
	}
}	
//
// Wednesday: Check the ads served to visitors on Wednesday.
//
if($day_of_the_week == "Wednesday"){
	$web_output = $web_output."<br><font face=\"Tahoma\" size=\"3\"><b>".$full_date."</b></font><br><br>"; 
	$commandline_output = $commandline_output."\n".$full_date."\n\n"; 
	
	$i=1;
	$response_status[] = array(1 => "pass",2 => "pass",3 => "pass",4 => "pass",5 => "pass",6 => "pass",7 => "pass");

	while($i <= 7){
		$web_output = $web_output."<font face=\"Courier New\" size=\"3\"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Visitor $i</b> - $url[$i]<br>"; 
		$commandline_output = $commandline_output."     Visitor $i - $url[$i]\n"; 
	
		$output = MakeAdCall($url[$i],$cookiefile,$debug);
		$output_array = MakeOutputArray($output,$debug);
		$responses = parseResponse($output_array,">>>> Selected",$debug);
		$response_status[$i] = CheckResponse($responses,$wednesday_response_strings[$i],$web,$debug);
	
		$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Expected Results: </b>".$wednesday_response_strings[$i]."<br>"; 
		$commandline_output = $commandline_output."     Expected Results: ".$wednesday_response_strings[$i]."\n"; 

		$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Actual Results: </b>".$responses."<br>"; 
		$commandline_output = $commandline_output."       Actual Results: ".trim($responses)."\n"; 
		if($response_status[$i] == "pass"){ $display_status = "<font color=\"green\"><b>Pass</b></font>"; }
		else if ($response_status[$i] == "fail"){ $display_status = "<font color=\"red\"><b>Fail</b></font>"; }
		$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Test Status: </b>".$display_status."<br>"; 
		$commandline_output = $commandline_output."          Test Status: ".$response_status[$i]."\n"; 
		$web_output = $web_output."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-----<br>"; 
		$commandline_output = $commandline_output."     -----\n"; 
		$i++;
	}
}	
//
// Thursday: Check the ads served to visitors on Thursday.
//
if($day_of_the_week == "Thursday"){
	$web_output = $web_output."<br><font face=\"Tahoma\" size=\"3\"><b>".$full_date."</b></font><br><br>"; 
	$commandline_output = $commandline_output."\n".$full_date."\n\n"; 
	
	$i=1;
	$response_status[] = array(1 => "pass",2 => "pass",3 => "pass",4 => "pass",5 => "pass",6 => "pass",7 => "pass");

	while($i <= 7){
		$web_output = $web_output."<font face=\"Courier New\" size=\"3\"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Visitor $i</b> - $url[$i]<br>"; 
		$commandline_output = $commandline_output."     Visitor $i - $url[$i]\n"; 
	
		$output = MakeAdCall($url[$i],$cookiefile,$debug);
		$output_array = MakeOutputArray($output,$debug);
		$responses = parseResponse($output_array,">>>> Selected",$debug);
		$response_status[$i] = CheckResponse($responses,$thursday_response_strings[$i],$web,$debug);
	
		$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Expected Results: </b>".$thursday_response_strings[$i]."<br>"; 
		$commandline_output = $commandline_output."     Expected Results: ".$thursday_response_strings[$i]."\n"; 

		$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Actual Results: </b>".$responses."<br>"; 
		$commandline_output = $commandline_output."       Actual Results: ".trim($responses)."\n"; 
		if($response_status[$i] == "pass"){ $display_status = "<font color=\"green\"><b>Pass</b></font>"; }
		else if ($response_status[$i] == "fail"){ $display_status = "<font color=\"red\"><b>Fail</b></font>"; }
		$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Test Status: </b>".$display_status."<br>"; 
		$commandline_output = $commandline_output."          Test Status: ".$response_status[$i]."\n"; 
		$web_output = $web_output."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-----<br>"; 
		$commandline_output = $commandline_output."     -----\n"; 
		$i++;
	}
}	
//
// Friday: Check the ads served to visitors on Friday.
//
if($day_of_the_week == "Friday"){
	$web_output = $web_output."<br><font face=\"Tahoma\" size=\"3\"><b>".$full_date."</b></font><br><br>"; 
	$commandline_output = $commandline_output."\n".$full_date."\n\n"; 
	
	$i=1;
	$response_status[] = array(1 => "pass",2 => "pass",3 => "pass",4 => "pass",5 => "pass",6 => "pass",7 => "pass");

	while($i <= 7){
		$web_output = $web_output."<font face=\"Courier New\" size=\"3\"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Visitor $i</b> - $url[$i]<br>"; 
		$commandline_output = $commandline_output."     Visitor $i - $url[$i]\n"; 
	
		$output = MakeAdCall($url[$i],$cookiefile,$debug);
		$output_array = MakeOutputArray($output,$debug);
		$responses = parseResponse($output_array,">>>> Selected",$debug);
		$response_status[$i] = CheckResponse($responses,$friday_response_strings[$i],$web,$debug);
	
		$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Expected Results: </b>".$friday_response_strings[$i]."<br>"; 
		$commandline_output = $commandline_output."     Expected Results: ".$friday_response_strings[$i]."\n"; 

		$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Actual Results: </b>".$responses."<br>"; 
		$commandline_output = $commandline_output."       Actual Results: ".trim($responses)."\n"; 
		if($response_status[$i] == "pass"){ $display_status = "<font color=\"green\"><b>Pass</b></font>"; }
		else if ($response_status[$i] == "fail"){ $display_status = "<font color=\"red\"><b>Fail</b></font>"; }
		$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Test Status: </b>".$display_status."<br>"; 
		$commandline_output = $commandline_output."          Test Status: ".$response_status[$i]."\n"; 
		$web_output = $web_output."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-----<br>"; 
		$commandline_output = $commandline_output."     -----\n"; 
		$i++;
	}
}	
//
// Saturday: Check the ads served to visitors on Saturday.
//
if($day_of_the_week == "Saturday"){
	$web_output = $web_output."<br><font face=\"Tahoma\" size=\"3\"><b>".$full_date."</b></font><br><br>"; 
	$commandline_output = $commandline_output."\n".$full_date."\n\n"; 
	
	$i=1;
	$response_status[] = array(1 => "pass",2 => "pass",3 => "pass",4 => "pass",5 => "pass",6 => "pass",7 => "pass");

	while($i <= 7){
		$web_output = $web_output."<font face=\"Courier New\" size=\"3\"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Visitor $i</b> - $url[$i]<br>"; 
		$commandline_output = $commandline_output."     Visitor $i - $url[$i]\n"; 
	
		$output = MakeAdCall($url[$i],$cookiefile,$debug);
		$output_array = MakeOutputArray($output,$debug);
		$responses = parseResponse($output_array,">>>> Selected",$debug);
		$response_status[$i] = CheckResponse($responses,$saturday_response_strings[$i],$web,$debug);
	
		$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Expected Results: </b>".$saturday_response_strings[$i]."<br>"; 
		$commandline_output = $commandline_output."     Expected Results: ".$saturday_response_strings[$i]."\n"; 

		$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Actual Results: </b>".$responses."<br>"; 
		$commandline_output = $commandline_output."       Actual Results: ".trim($responses)."\n"; 
		if($response_status[$i] == "pass"){ $display_status = "<font color=\"green\"><b>Pass</b></font>"; }
		else if ($response_status[$i] == "fail"){ $display_status = "<font color=\"red\"><b>Fail</b></font>"; }
		$web_output = $web_output."<b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Test Status: </b>".$display_status."<br>"; 
		$commandline_output = $commandline_output."          Test Status: ".$response_status[$i]."\n"; 
		$web_output = $web_output."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-----<br>"; 
		$commandline_output = $commandline_output."     -----\n"; 
		$i++;
	}
}	

if ($response_status[1]=="pass" && $response_status[2]=="pass" && $response_status[3]=="pass" && $response_status[4]=="pass" &&
    $response_status[5]=="pass" && $response_status[6]=="pass" && $response_status[7]=="pass"){
	$web_status_output = $web_status_output."<font face=\"Tahoma\" size=\"3\" color=\"green\"><b>***** SUCCESS - Behavioral Targeting end-to-end responses are valid for Visitors 1 through 7</b></font><br><br><hr>"; 
	$commandline_status_output = $commandline_status_output."\n***** SUCCESS - Behavioral Targeting end-to-end responses are valid for Visitors 1 through 7\n\n"; 
	$status = "pass"; 
}
else {
	$web_status_output = $web_status_output."<font face=\"Tahoma\" size=\"3\" color=\"red\"><b>***** FAIL - Some or All Behavioral Targeting end-to-end responses are not valid</b></font><br><br><hr>"; 
	$commandline_status_output = $commandline_status_output."\n***** FAIL - Some or All Behavioral Targeting end-to-end responses are not valid\n\n"; 
	$status = "fail"; 
}


	if (isset($web)) { 
		echo $web_status_output; 
		echo $web_output; }
	else {
		echo $commandline_status_output;
		echo $commandline_output; }

$filestatus = WriteResultsFile($status,$testshortname,$testname,$testdesc,$testurl,$testtime,$web,$commandline_status_output,$commandline_output,$output);

if (isset($debug)) { phpinfo(); }

//================= Functions =================
	
function WriteResultsFile($status,$testshortname,$testname,$testdesc,$testurl,$testtime,$web,$commandline_status_output,$commandline_output,$output){
	// Setup the archival file name for the test
	$datestamp = date("YmdGis");
	$directory = "/http/matchtest/archived_results/btendtoendtest/";
	if(!is_dir($directory)){
		if(!mkdir($directory, 0777)) {
			die('Failed to create directory: '.$directory);
		}
	}

	// Write out the information to the testresults.txt file.
	$lines = file('/http/matchtest/testresults.txt');
	$numitems = count($lines);

	// Loop through our array, show HTML source as HTML source; and line numbers too.

	if (isset($debug)) { echo "<br><pre>HTTP Response Output:<br><br>"; print_r($lines); echo "</pre><br>"; }

	$newfile = $lines[0];
	$i=1;
	while($i <= $numitems) {
		$pieces = explode("|",$lines[$i]);
		if($testname == $pieces[1]){
			$replaced_line = $status."|".$testname."|".$testdesc."|".$testurl."|".$testtime."\n";
			$newfile = $newfile.$replaced_line;
		}
		else{
			$newfile = $newfile.$lines[$i];
		}
		$i++;
	}

	if (isset($debug)) { echo "<br><pre>New File Output:<br><br>"; print_r($newfile); echo "</pre><br>"; }

	$fp = fopen('/http/matchtest/testresults.txt', 'w');

	fwrite($fp, $newfile);
	fclose($fp);

// Write out results of the test to a file 
	if (isset($web)) { $testfilename = $directory.$testshortname.".web.".$status.".".$datestamp.".".$testtime.".txt"; }
	else { $testfilename = $directory.$testshortname.".commandline.".$status.".".$datestamp.".".$testtime.".txt"; }

	if (!$fp1 = fopen($testfilename,'w')) {
		echo "Cannot open file ($testfilename)";
		exit;
	}

	fwrite ($fp1,$commandline_status_output."\n\n");
	fwrite ($fp1,$commandline_output."\n\n");
//	fwrite ($fp1,"\n\n".$message3."\n\n".$output."\n\n");
	fclose($fp1);

	return 0;
}

function MakeAdCall($url,$cookiefile,$debug) {
	$UserAgent="Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16";

    if (isset($debug)) { echo "URL: ".$url."<br>"; }

    // initilize the curl call
    $ch = curl_init();
    // set useragent
    curl_setopt($ch, CURLOPT_USERAGENT, $UserAgent);
	// Read Cookies from Cookiefile
	curl_setopt ($ch, CURLOPT_COOKIEFILE, '/http/matchtest/tests/cookiefile.txt'); 
    // Set curl to use $url for http call
    curl_setopt($ch, CURLOPT_URL, $url);
    // show client headers
    curl_setopt($ch, CURLOPT_HEADER, 1);
    // have curl put X-Forwarded-For into headers
    curl_setopt($ch,CURLOPT_HTTPHEADER, $client_headers);
    // store results as the return of curl_exec
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    // Follow redirects
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);		
    // if HTML error 400 over over, fail
    curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
	// Save Cookies in Cookiefile
	curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookiefile); 

    $output = curl_exec($ch);
    if (isset($debug)) { echo "HTTP Response Output:<br><pre>"; print_r($output); echo "</pre><br>"; }
    //close curl connection
    curl_close($ch);

return $output;
}


function MakeOutputArray($output,$debug){
        $output_array = preg_split('/[\n]/', $output, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
        if (isset($debug)) { echo "HTTP Response Output (array):<br><pre>"; print_r($output_array); echo "</pre><br><br>"; }

return $output_array;
}


function Response($output_array,$string,$debug) {

        $line_match = array_values(preg_grep("/$string/", $output_array));
            if (isset($GLOBALS["debug"])) { echo "DEBUG - string: <pre>".$string."</pre><br>";}
            if (isset($GLOBALS["debug"])) { echo "DEBUG - output:  <br><pre>"; print_r($output_array); echo "</pre><br>";}
            if (isset($GLOBALS["debug"])) { echo "DEBUG - line_match:<br><pre>"; print_r($line_match); echo "</pre><br>";}
        if(stripos($line_match[0], " = ")){
			$explode = explode(" = ", $line_match[0]); }
        if(stripos($line_match[0], " ")){
			$explode_a = explode(" ", trim($line_match[0]));
			if (isset($debug)) { echo "\n\nRedirect To: ".$explode_a[1]."<br>"; }
			$result = $explode_a[1]; 
		}
        if(stripos($line_match[1], ">>>>")){
			$result = $line_match[1]; }
        if(stripos($line_match[0], ">>>>")){
			$result = $line_match[0]; }
        if (isset($GLOBALS["debug"])) { echo "DEBUG - result:  ".$result."<br>";}
		return $result;
}

function CheckResponse($first_response,$response_string,$web,$debug){
    if (isset($debug)) { echo "<br>First Response: ".$first_response."<br>"; }
    if (isset($debug)) { echo "<br>Response String: ".$response_string."<br>"; }

		if(strcasecmp(trim($first_response), $response_string)==0){
			$status = "pass";
        }
        else {
			$status = "fail";			
		}
	return $status;
}

function CheckCookieFile($cookiefile,$value,$second_response,$third_response,$fourth_response,$web,$debug){
	$line = file($cookiefile);
	if (isset($debug)) { echo "Value: ".$value."<br>"; }
	if (isset($debug)) { echo "Second Response: ".$second_response."<br>"; }
	if (isset($debug)) { echo "Third Response: ".$third_response."<br>"; }
	if (isset($debug)) { echo "Fourth Response: ".$fourth_response."<br>"; }
	if (isset($debug)) { echo "Cookie File Contents (array):<br><pre>"; print_r($line); echo "</pre><br>"; }

	$admeld_pieces = explode("\t",$line[4]);
	if (isset($debug)) { echo "Cookie File AdMeld Line Contents (array):<br><pre>"; print_r($admeld_pieces); echo "</pre><br>"; }
	if (trim($admeld_pieces[0]) == ".tag.admeld.com" && trim($admeld_pieces[5]) == "meld_sess" && trim($admeld_pieces[6]) == trim($value)) {
		$admeld_status = "pass"; }
	else {
		$admeld_status = "fail"; }
	if (isset($debug)) { echo "AdMeld Status: ".$admeld_status."<br>"; }

	$aimatch_pieces = explode("\t",$line[5]);
	if (isset($debug)) { echo "Cookie File aiMatch Line Contents (array):<br><pre>"; print_r($aimatch_pieces); echo "</pre><br>"; }

	$aimatch_cookie_value = str_replace(";","",$third_response).str_replace(";","",$fourth_response);
	if (isset($debug)) { echo "aiMatch Cookie Value: ".$aimatch_cookie_value."<br>"; }
	
	if (trim($aimatch_pieces[0]) == ".aimatch.com" && trim($aimatch_pieces[5]) == "External" && trim($aimatch_pieces[6]) == trim($aimatch_cookie_value)) {
		$aimatch_status = "pass"; }
	else {
		$aimatch_status = "fail"; }
	if (isset($debug)) { echo "aiMatch Status: ".$aimatch_status."<br>"; }

	if ($aimatch_status == "pass" && $admeld_status == "pass"){
		$status = "pass"; }
	else {
		$status = "fail"; }

	if (isset($debug)) { echo "Cookie File Status: ".$aimatch_status."<br>"; }
		
return $status;
}

function parseResponse($output_array,$string,$debug) {
		$i=0;
        $num = count($output_array);
		while($i <= $num){
			if(stripos($output_array[$i],$string)){
				return $output_array[$i];
			}
		$i++;
		}
}

?>
